<?php
//strlen、mb_strlen求字符串长度
$s="123计算机";
echo strlen($s),"\n"; //12
echo mb_strlen($s),"\n"; //6
//strcmp用于比较两个字符串的大小
var_dump(strcmp("Hello","Hi"));//int(-1)
var_dump(strcmp("Hi","hi"));//int(-1)
var_dump(strcmp("Hi~","Hi"));//int(1)
var_dump(strcasecmp("Hi","hi"));//int(0) 忽略大小写
//trim、ltrim、rtrim删除字符串两侧，左侧、右侧的空白
$s="  \t \t How are you?  \t   ";
echo "[$s]\n"; //[  	 	 How are you?  	   ]
echo "[", trim($s), "]\n"; //[How are you?]
echo "[", ltrim($s), "]\n"; //[How are you?  	   ]
echo "[", rtrim($s), "]\n"; //  	 	 How are you?]
echo "[", trim(",.;'Hello___","_.,';"), "]\n"; //[Hello]
//strpos函数返回在字符串haystack中needle首次出现的从0开始的位置
$s="How old are you?";
echo strpos($s,"o"),"\n"; //1
echo strrpos($s,"o"),"\n"; //13
$pos=0;
echo $pos=strpos($s,"o", $pos),"\n"; //1
echo $pos=strpos($s,"o", ++$pos),"\n"; //4
echo $pos=strpos($s,"o", ++$pos),"\n"; //13
var_dump($pos=strpos($s,"o", ++$pos)); //bool(false) 没有找到
//bin2hex将字符串的每个字节转换为两个16进制位
$a='计算机123'; //E8 AE A1 E7 AE 97 E6 9C BA 31 32 33
echo bin2hex($a),"\n"; //e8aea1e7ae97e69cba313233
echo hex2bin('e8aea1e7ae97e69cba313233'),"\n";
//chunk_split把字符串用指定的字符分隔开来
echo chunk_split("123456",2,":"),"\n"; //12:34:56:
echo chunk_split("e8aea1e7ae97e69cba313233",2," "),"\n"; //e8 ae a1 e7 ae 97 e6 9c ba 31 32 33
echo chunk_split("e8aea1e7ae97e69cba313233",2,"_"),"\n"; //e8_ae_a1_e7_ae_97_e6_9c_ba_31_32_33_
//str_replace字符串替换
$s="How old are you?";
echo str_replace('o','**',$s),"\n";//H**w **ld are y**u?
//从字符串中，不管找到o还是r，统统替换成*
echo str_replace(['o','r'],'*',$s),"\n";//H*w *ld a*e y*u?
//字符串中，找到o替换成O, 找到r替换成R
echo str_replace(['o','r'],['O','R'],$s),"\n";//HOw Old aRe yOu?
//两个字符串，都按上面的规则处理
print_r(str_replace(['o','r'],['O','R'],[$s,'Hero']));
//  Array
//  (
//      [0] => HOw Old aRe yOu?
//      [1] => HeRO
//  )
echo str_replace(' ','',$s),"\n"; //Howoldareyou?
//str_repeat多次重复字符串
echo str_repeat('Ok',3),"\n"; //OkOkOk
echo '[',str_repeat('Ok',0),"]\n"; //[]
//join将数组元素拼接成字符串
echo join(['Hi','Zhangsan']),"\n"; //HiZhangsan
echo join(', ', [123,456,789]),"\n"; //123, 456, 789
//strtolower、strtoupper转换成小写、大写
echo strtolower("How Are You.\n"); //how are you.
echo strtoupper("I'm fine, Thank you.\n"); //I'M FINE, THANK YOU.
echo strtoupper("e8_ae_a1_e7_ae_97\n"); //E8_AE_A1_E7_AE_97
//printf显示格式化字符串
$a=32.856;
printf("[%f] [%1$.1f] [%1$6.1f] [%1$-6.1f]\n", $a); //[32.856000] [32.9] [  32.9] [32.9  ]
$s="Hello World!";
printf("[%s] [%1$.5s] [%1$8.5s] [%1$'-8.5s]\n", $s); //[Hello World!] [Hello] [   Hello] [---Hello]
$s="汉字";
printf("[%s] [%1$.3s] [%1$6.3s] [%1$'-8.3s]\n", $s); //[汉字] [汉] [   汉] [-----汉]
//substr取子串
echo substr("Hello",0,3),"\n"; //Hel
echo substr("Hello",2),"\n"; //llo
//-3表示字符串后边的3个字符不要了
echo substr("Hello",0,-3),"\n"; //He
//-4表示从倒数第4个字符开始取子串
echo substr("Hello",-4,-2),"\n"; //el
//str_starts_with、str_ends_with 以子串开始或结束
var_dump(str_starts_with("Hello","ll")); //bool(false)
var_dump(str_starts_with("Hello","he")); //bool(false)
var_dump(str_starts_with("Hello","He")); //bool(true)
var_dump(str_ends_with("Hello","llo")); //bool(true)

//将数组中的每项数据及其对应的 16 进制串，两端对齐显示在指定的宽度内，若宽度不足，则两项数据直接显示。
$a=['张三丰','里  斯','阿 kit','papi 酱','Jackson'];
outFormat(30,$a); //利用函数处理数组数据, 输出内容在35个英文字符宽度内

//完善函数功能，将数组中的每项数据及其16进制串，两端对齐显示在指定的宽度内
//数组中的每一项数据中包含的空格要删除掉
//16进制串中每个字节占两个16进制位，英文字符采用大写，各字节间以_分隔
//本文件存储采用utf-8编码
function outFormat(int $width, ?array $a):void {
    if(is_null($a))return;
    foreach($a as $name) {
        $name=str_replace(' ','',$name); //删除姓名中的空白  里斯
        $hex=bin2hex($name); //把字符串每个字节转换为2个16进制位 e9878ce696af
        $hex=strtoupper($hex); //转换成大写 E9878CE696AF
        $hex=chunk_split($hex,2,"_"); //用_分隔每个字节 E9_87_8C_E6_96_AF_
        $hex=substr($hex,0,-1); //删除最后的_  E9_87_8C_E6_96_AF
        $lenName=strlen(iconv('utf-8','gbk',$name)); //显示姓名时所占的宽度
        $lenHex=strlen($hex); //16进制串的长度
        $lenSpace=$width-$lenName-$lenHex; //中间空格的宽度
        if($lenSpace<0)$lenSpace=0;//宽度的控制
        echo $name, str_repeat(' ',$lenSpace), $hex,"\n";
    }

}
/* 输出内容如下：
张三丰   E5_BC_A0_E4_B8_89_E4_B8_B0
里斯              E9_87_8C_E6_96_AF
阿kit             E9_98_BF_6B_69_74
papi酱         70_61_70_69_E9_85_B1
Jackson        4A_61_63_6B_73_6F_6E
*/
